﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.LocalRigidbodyFixer
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

#nullable disable
namespace Netick.Unity;

internal class LocalRigidbodyFixer
{
  internal readonly NetworkSandbox Sandbox;
  internal readonly List<TrackedRigidbodyBase> Rigidbodies = new List<TrackedRigidbodyBase>(512 /*0x0200*/);
  private List<Rigidbody> _cachedList3D = new List<Rigidbody>(512 /*0x0200*/);
  private List<Rigidbody2D> _cachedList2D = new List<Rigidbody2D>(512 /*0x0200*/);
  private List<TrackedRigidbodyBase> _cachedListTracked = new List<TrackedRigidbodyBase>(512 /*0x0200*/);

  internal LocalRigidbodyFixer(NetworkSandbox sandbox) => this.Sandbox = sandbox;

  internal void PreRollback()
  {
    if (!this.Sandbox.PhysicsPrediction || this.Sandbox.PhysicsType == PhysicsType.None)
      return;
    foreach (TrackedRigidbodyBase rigidbody in this.Rigidbodies)
      rigidbody.PreRollback();
  }

  internal void PostResimulation()
  {
    if (!this.Sandbox.PhysicsPrediction || this.Sandbox.PhysicsType == PhysicsType.None)
      return;
    foreach (TrackedRigidbodyBase rigidbody in this.Rigidbodies)
      rigidbody.PostResimulation();
  }

  internal void PostSimulation()
  {
    if (!this.Sandbox.PhysicsPrediction || this.Sandbox.PhysicsType == PhysicsType.None)
      return;
    foreach (TrackedRigidbodyBase rigidbody in this.Rigidbodies)
      rigidbody.PostSimulation();
  }

  internal void Interpolate(float alpha)
  {
    if (!this.Sandbox.PhysicsPrediction || this.Sandbox.PhysicsType == PhysicsType.None)
      return;
    foreach (TrackedRigidbodyBase rigidbody in this.Rigidbodies)
      rigidbody.Interpolate(alpha);
  }

  internal void FindAllRigidbodies(Scene scene)
  {
    Rigidbody[] objectsOfType1 = Object.FindObjectsOfType<Rigidbody>();
    Rigidbody2D[] objectsOfType2 = Object.FindObjectsOfType<Rigidbody2D>();
    if (this.Sandbox.Config.PhysicsType == PhysicsType.None)
      return;
    foreach (Rigidbody rigidbody in objectsOfType1)
    {
      if (rigidbody.gameObject.scene == scene && rigidbody.GetComponent<IPhysicsComponent>() == null)
        this.AddRigidbody(rigidbody.gameObject);
    }
    foreach (Rigidbody2D rigidbody2D in objectsOfType2)
    {
      if (rigidbody2D.gameObject.scene == scene && rigidbody2D.GetComponent<IPhysicsComponent>() == null)
        this.AddRigidbody(rigidbody2D.gameObject);
    }
  }

  public void AddRigidbody(GameObject rigid)
  {
    Rigidbody component1 = rigid.GetComponent<Rigidbody>();
    Rigidbody2D component2 = rigid.GetComponent<Rigidbody2D>();
    TrackedRigidbodyBase trackedRigidbodyBase = (TrackedRigidbodyBase) null;
    if ((Object) rigid.GetComponent<TrackedRigidbodyBase>() != (Object) null)
      return;
    if ((Object) component1 != (Object) null && !component1.isKinematic)
      trackedRigidbodyBase = (TrackedRigidbodyBase) rigid.AddComponent<TrackedRigidbody3D>();
    if ((Object) component2 != (Object) null && component2.bodyType != RigidbodyType2D.Static && !component2.isKinematic)
      trackedRigidbodyBase = (TrackedRigidbodyBase) rigid.AddComponent<TrackedRigidbody2D>();
    if (!((Object) trackedRigidbodyBase != (Object) null))
      return;
    trackedRigidbodyBase.Init(this.Sandbox);
    this.Rigidbodies.Add(trackedRigidbodyBase);
  }

  internal void AddRigid(GameObject go)
  {
    if (this.Sandbox.Config.PhysicsType == PhysicsType.None)
      return;
    go.GetComponentsInChildren<Rigidbody>(this._cachedList3D);
    go.GetComponentsInChildren<Rigidbody2D>(this._cachedList2D);
    foreach (Rigidbody rigidbody in this._cachedList3D)
    {
      if (rigidbody.GetComponent<IPhysicsComponent>() == null)
        this.AddRigidbody(rigidbody.gameObject);
    }
    foreach (Rigidbody2D rigidbody2D in this._cachedList2D)
    {
      if (rigidbody2D.GetComponent<IPhysicsComponent>() == null)
        this.AddRigidbody(rigidbody2D.gameObject);
    }
    this._cachedList3D.Clear();
    this._cachedList2D.Clear();
  }

  internal void Remove(GameObject go)
  {
    this._cachedListTracked.Clear();
    go.GetComponentsInChildren<TrackedRigidbodyBase>(this._cachedListTracked);
    foreach (TrackedRigidbodyBase trackedRigidbodyBase in this._cachedListTracked)
      this.Rigidbodies.Remove(trackedRigidbodyBase);
    this._cachedListTracked.Clear();
  }

  public void RemoveRigidbody(GameObject rigid)
  {
    this.Rigidbodies.Remove(rigid.gameObject.GetComponent<TrackedRigidbodyBase>());
  }
}
